package com.example.myapplication;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.table.TableData;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;

public class Show_Table extends Dialog {
    public ExecutorService exec = null;
    private DBUtils mysql_db = null;

    private Button button_commit;
    private Button button_quit;
    private EditText edit_query;
    private ListView listView;
    private TextView text_table_title;

    //标记按钮编号
    int flag;

    public Show_Table(@NonNull Context context,ExecutorService e,DBUtils m,int index) {
        super(context);
        exec = e;
        mysql_db = m;
        flag = index;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_table);
        button_commit = findViewById(R.id.button_commit);
        button_quit = findViewById(R.id.button_quit);
        edit_query = findViewById(R.id.edit_query);
        listView = findViewById(R.id.table);
        text_table_title = findViewById(R.id.text_table_title);
        if(flag==1){
            text_table_title.setText("按借阅人查询");
        }else if(flag==2){
            text_table_title.setText("按书号查询");
        }

        // 确认按钮监听
        button_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> data = new ArrayList<>();
                String query = edit_query.getText().toString();
                exec.execute(new Runnable() {
                    @Override
                    public void run() {
                        mysql_db.TableQuery(query, data,flag);
                    }
                });
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        getContext(), android.R.layout.simple_list_item_1, data
                );
                listView.setAdapter(adapter);
                listView.invalidate();
            }
        });
        // 退出按钮监听
        button_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}


